import { _decorator, Component, instantiate, Node } from 'cc';
import { BaseComponent } from '../../../Manager/UIManager/BaseComponent';
import { GAME_PREFAB } from '../../../Data/GlobalData';
import { SingleLineBun } from './SingleLineBun';
import { BunManger } from '../../../Tool/BunManger';
import { StickLoad } from '../StickLoad/StickLoad';
import { GameScene } from '../GameScene';
import { SingleBrick } from '../StickLoad/SingleBrick';
import { Bun } from './Bun';
import { Tools } from '../../../Tool/Tools';
const { ccclass, property } = _decorator;

@ccclass('PlateLoad')
/**
 * 盘子层
 */
export class PlateLoad extends BaseComponent {

    //单排豆包层
    @property(Node)
    private SingleBunLoad: Node = null;

    //单排豆包得数组
    private SingleBunArray: Node[] = [];

    //豆包排数
    private BunRow: number = 5;

    //游戏场景
    @property(Node)
    private GameScene: Node = null;

    //筷子层
    @property(Node)
    private StickLoad: Node = null;

    protected Init(): void {
        for (let i = 0; i < this.BunRow; i++) {
            let sb: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.SINGLELINEBUN]);
            this.SingleBunLoad.addChild(sb);
            sb.getComponent(SingleLineBun).PlateLoad = this;
            this.SingleBunArray.push(sb);
        }
    }

    public UpdateLevel(): void {
        for (let sb of this.SingleBunArray) {
            let bun_indexs: number[] = [];
            for (let i = 0; i < 3; i++)bun_indexs.push(BunManger.instance.ReturnBunNum());
            sb.getComponent(SingleLineBun).Init(bun_indexs);
        }
    }

    /**
     * 抓豆包
     */
    public DrawBun(_bunindex: number) {
        for (let sb of this.SingleBunArray) {
            if (sb.getComponent(SingleLineBun).CheckClear(_bunindex, this.StickLoad.getComponent(StickLoad).ChooseStick[0].node.getWorldPosition(), this.StickLoad.getComponent(StickLoad).ChooseStick[1].node.getWorldPosition())) {
                this.StickLoad.getComponent(StickLoad).StickReset(true);
                return;
            }
        }

        this.StickLoad.getComponent(StickLoad).StickReset(false);
    }

    /**
     * 判断游戏是否结束
     */
    public CheckGameOver() {
        console.log("判断游戏是否结束")
        let is_oever: boolean = true;

        for (let sb of this.SingleBunArray) {
            console.log("这一列是否结束", sb.getComponent(SingleLineBun).CheckIsEnd())
            if (!sb.getComponent(SingleLineBun).CheckIsEnd()) {
                is_oever = false;
                break;
            }
        }
        console.log("游戏是否结束了？", is_oever)
        if (is_oever) {
            this.GameScene.getComponent(GameScene).UpdateLevel();
        }
    }

    /**
     * 获取金筷子
     */
    private GetGoldStick() {
        for (let sb of this.SingleBunArray) {
            sb.getComponent(SingleLineBun).FirstRowClear();
        }
    }

    /**
     * 随机打乱顺寻
     */
    public RandomShuffle() {
        let bunnum_arr: number[] = [];
        let bun_arr: Node[] = [];

        for (let sb of this.SingleBunArray) {
            for (let bun of sb.children) {
                if (bun.getComponent(Bun).BunIndex != -1) {
                    bunnum_arr.push(bun.getComponent(Bun).BunIndex);
                    bun_arr.push(bun);
                }
            }
        };

        bunnum_arr = Tools.shuffleArray(bunnum_arr);

        for (let bun of bun_arr) {
            let ran_num: number = Math.floor(Math.random() * bunnum_arr.length)
            bun.getComponent(Bun).UpdateBun(bunnum_arr[ran_num]);
            bunnum_arr.splice(ran_num, 1);
        }

        for (let sb of this.SingleBunArray) {
            sb.getComponent(SingleLineBun).CheckShowShadow();
        };
    }

}


